﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BlogsManage
{
    public partial class frmLogin : Form
    {
        public string u_password;
        public string u_name;
        //数据库帮助类
        DBhelp db = new DBhelp();
        //创建数据集
        DataSet ds = new DataSet();
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {

        }

        public void Login()
        {
            int i = 0;

            string sql = string.Format("SELECT Id FROM Users WHERE Username='{0}' AND  Password='{1}'", this.txtUsername.Text, this.txtPassword.Text);
            if (db.GetScalar(sql.ToString()) == null)
            {
                MessageBox.Show("登陆失败");
                return;

            }
            i = (int)db.GetScalar(sql.ToString());
            u_name = this.txtUsername.Text;
            u_password = this.txtPassword.Text;


            if (i != 0)
            {

                frmMain frm = new frmMain();
                frm.Show();
                this.Hide();
            }

        }


        /// <summary>
        /// 非空验证
        /// </summary>
        /// <returns></returns>
        public Boolean Ck()
        {
            if (this.txtUsername.Text == "")
            {
                MessageBox.Show("输入用户名");
                return false;
            }
            else if (this.txtPassword.Text == "")
            {
                MessageBox.Show("输入密码");
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (Ck() == true)
            {
                Login();
            }
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            frmRegister frg = new frmRegister();

            frg.Show();
            this.Hide();
        }
    }
}
